/**
 * @author KujoMiva
 * @name CheckMap.ts
 * @desc 选择地图界面
 * @time 2020年02月10日 19:02:34 星期一
 */
class CheckMap extends egret.DisplayObjectContainer {
  private switchSpeed = 800
  private animationSpeed = 300
  private canToogel = true
  public constructor() {
    super()
    this.once(egret.Event.ADDED_TO_STAGE, this.init, this)
  }

  private init() {
    this.createBg()
    this.createMap()
    this.createController()
    this.createBtnSubmit()
    this.createHeart()
    this.createTip()
  }

  private createBg() {
    const bg = Utils.createBitmapByName('bg_card_png')
    Utils.anchorCenter(bg)
    Utils.createScale(bg, this)
    bg.scaleX = bg.scaleX - .03
    bg.scaleY = bg.scaleY - .03
    bg.x = this.stage.stageWidth / 2
    bg.y = this.stage.stageHeight / 2
  }

  private createMap() {
    const container = new egret.Sprite()
    const map = Utils.createBitmapByName('map_1_png')
    container.addChild(map)
    const map_3 = Utils.createBitmapByName(`map_3_act_${ParamsPool.PATH_NUM}_png`)
    container.addChild(map_3)
    map_3.alpha = 0

    egret.Tween.get(map)
      .wait(1000)
      .wait(this.switchSpeed)
      .to({ alpha: 0 }, this.switchSpeed)
      .call(() => {
        map.texture = RES.getRes('map_2_png')
      })
      .to({ alpha: 1 }, this.switchSpeed)
      .wait(this.switchSpeed)
      .to({ alpha: 0 }, this.switchSpeed)
      .call(() => {
        map.texture = RES.getRes('map_3_card_png')
        egret.Tween.get(map_3).to({ alpha: 1 }, this.switchSpeed)
      })
      .to({ alpha: 1 }, this.switchSpeed)


    Utils.createScale(container, this)
    container.anchorOffsetX = container.width / 2
    container.x = this.stage.stageWidth / 2
    container.y = 50
    map_3.y = 95

    this.addEventListener('toogelMap_3', () => {
      map_3.texture = RES.getRes(`map_3_act_${ParamsPool.PATH_NUM}_png`)
    }, this)

  }

  private createController() {
    const container = new egret.Sprite()
    const text_png = Utils.createBitmapByName('loc_1_png')
    const { x: TX } = text_png
    container.addChild(text_png)

    Utils.createScale(container, this)
    container.anchorOffsetX = container.width / 2
    container.x = this.stage.stageWidth / 2
    container.y = 735

    const btn_left = Utils.createBitmapByName('left_png')
    Utils.createScale(btn_left, this, 1)
    btn_left.x = 65
    btn_left.touchEnabled = true
    btn_left.addEventListener(egret.TouchEvent.TOUCH_TAP, () => {
      if (!this.canToogel) {
        console.log('wait')
        return
      }
      this.canToogel = false
      ParamsPool.PATH_NUM--
      if (ParamsPool.PATH_NUM < 1) {
        ParamsPool.PATH_NUM = 8
      }
      egret.Tween.get(text_png)
        .to({ x: TX + 200, alpha: 0 }, this.animationSpeed)
        .call(() => {
          text_png.texture = RES.getRes(`loc_${ParamsPool.PATH_NUM}_png`)
        })
        .to({ x: TX - 100 })
        .to({ x: TX, alpha: 1 }, this.animationSpeed)
        .call(() => {
          this.canToogel = true
        })
      this.dispatchEvent(new egret.Event('toogelMap_3'))
    }, this)


    const btn_right = Utils.createBitmapByName('Right_png')
    Utils.createScale(btn_right, this, 1)
    btn_right.x = 530
    btn_right.touchEnabled = true
    btn_right.addEventListener(egret.TouchEvent.TOUCH_TAP, () => {
      if (!this.canToogel) {
        console.log('wait')
        return
      }
      this.canToogel = false
      ParamsPool.PATH_NUM++
      if (ParamsPool.PATH_NUM > 8) {
        ParamsPool.PATH_NUM = 1
      }

      egret.Tween.get(text_png)
        .to({ x: TX - 200, alpha: 0 }, this.animationSpeed)
        .call(() => {
          text_png.texture = RES.getRes(`loc_${ParamsPool.PATH_NUM}_png`)
        })
        .to({ x: TX + 100 })
        .to({ x: TX, alpha: 1 }, this.animationSpeed)
        .call(() => {
          this.canToogel = true
        })
      this.dispatchEvent(new egret.Event('toogelMap_3'))
    }, this)

    btn_left.y = btn_right.y = 825

    const text = Utils.createText(`${Number(ParamsPool.PLAYER_NUM) + 13028}`, { x: 333, y: 947 }, this, false)
    Utils.anchorCenter(text)
    text.x = this.stage.stageWidth / 2 + 5
    text.bold = false
    text.stroke = 0
    text.size = 38
  }

  private createBtnSubmit() {
    const btn = Utils.createBitmapByName('btn_choose_loc_png')
    Utils.createScale(btn, this)
    Utils.anchorCenter(btn)
    btn.x = this.stage.stageWidth / 2
    btn.y = 1020
    btn.touchEnabled = true
    btn.addEventListener(egret.TouchEvent.TOUCH_TAP, () => {
      console.log('选择了地区:', ParamsPool.PATH_NUM)
      // this.stage.dispatchEvent(new egret.Event('gotoCheckText'))
      this.dispatchEvent(new egret.Event('next'))

      // egret.Tween.get(this).to({ alpha: 0 }, 1000).call(() => this.parent.removeChild(this))
    }, this)
  }

  private createHeart() {
    const heart = Utils.createBitmapByName('item_heart_png')
    Utils.createScale(heart, this, .05)
    Utils.anchorCenter(heart)
    heart.x = 320
    heart.y = 693
    heart.alpha = 0
    this.addEventListener('next', () => {
      egret.Tween.get(heart)
        .to({ x: 205, y: 1000, alpha: 1 }, 800)
        .call(() => {
          this.stage.dispatchEvent(new egret.Event('gotoCheckText'))
          console.log('gotoCheckText')
          egret.Tween.get(this).to({ alpha: 0 }, 1000).call(() => this.parent.removeChild(this))
        })
    }, this)
  }

  private createTip() {
    const tip = Utils.createBitmapByName('map_tip_png')
    Utils.itemScale(tip)
    this.addChild(tip)
    // Utils.dragGetPatn(tip,this)
    tip.x = 395
    tip.y = 65

  }

}